package com.yanqu.road.server.cmd.activity.unionwar;


import com.yanqu.road.entity.activity.unionwar.UnionWarDispatchDetail;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.logic.pb.UnionBasePb;
import com.yanqu.road.pb.activity.unionwar.CrossUnionWarProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.manger.activity.unionwar.UnionPhaseWarMgr;
import com.yanqu.road.server.manger.activity.unionwar.UnionWar;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;
import io.netty.channel.ChannelHandlerContext;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.S_CROSS_UNION_WAR_MATCH_INFO_SYNC_FORM_CROSS,desc = "对阵匹配信息从跨服下发")
public class UnionWarMatchInfoSyncCmdFromCross implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        CrossUnionWarProto.CrossUnionWarMatchInfoSendMsg msg = CrossUnionWarProto.CrossUnionWarMatchInfoSendMsg.parseFrom(packet.getBytes());
        UnionWar unionWar = UnionPhaseWarMgr.getUnionWarMap().get(msg.getActivityId());
        List<String> unionUidList = new ArrayList<>();
        if(unionWar != null) {
            for(CrossUnionWarProto.CroUnionWarMatchInfoTemp temp : msg.getMatchList()){
                UnionWarDispatchDetail detail = unionWar.getUnionWarDispatchDetailList(temp.getUnionUid(), msg.getPhase(), msg.getSession());
                if(detail != null){
                    //long enemyServerId = temp.getEnemyServerId();
                    synchronized (detail) {
                        if(!StringUtils.isNullOrEmpty(temp.getEnemyUnionUid())) {
                            detail.setEnemyUnionUid(temp.getEnemyUnionUid());
                            detail.setEnemyUnionInfo(UnionBasePb.parseUnionBaseInfo(temp.getEnemyBaseInfo()));
                        }
                        unionUidList.add(detail.getUnionUid());
                    }
                }else{
                    if(msg.getPhase() == 2){  //决赛的备战由跨服下发生成；//积分赛的备战数据由区服生成上传
                        Object lock = unionWar.getUnionLocker(temp.getUnionUid());
                        synchronized (lock) {
                            detail = unionWar.checkAndGetUnionWarDispatchDetails(temp.getUnionUid(), msg.getPhase(), msg.getSession());
                            detail.setGroupId(msg.getGroupId());
                            if(!StringUtils.isNullOrEmpty(temp.getEnemyUnionUid())) {
                                detail.setEnemyUnionUid(temp.getEnemyUnionUid());
                                detail.setEnemyUnionInfo(UnionBasePb.parseUnionBaseInfo(temp.getEnemyBaseInfo()));
                            }
                            detail.setWinMark(temp.getWinMark());
                        }
                        unionUidList.add(detail.getUnionUid());
                    }else if(msg.getPhase() == 1){
                        //第一阶段只接收同分组 detail
                        unionUidList.add(temp.getUnionUid());
                    }
                }
            }
        }
        if(unionUidList.size() > 0){
            CrossUnionWarProto.CroUnionWarMatchInfoBackMsg.Builder backMsg = CrossUnionWarProto.CroUnionWarMatchInfoBackMsg.newBuilder();
            backMsg.setActivityId(msg.getActivityId());
            backMsg.setPhase(msg.getPhase());
            backMsg.setSession(msg.getSession());
            backMsg.addAllUnionUid(unionUidList);
            backMsg.setGroupId(msg.getGroupId());
            //GamePlayerMgr.sendChannelPacket(0, YanQuMessageUtils.buildMessage(Protocol.C_CROSS_UNION_WAR_MATCH_INFO_SYNC_BACK, backMsg));
            GamePlayerMgr.sendUnionActivityGroupPacket(Protocol.C_CROSS_UNION_WAR_MATCH_INFO_SYNC_BACK, backMsg, 0, msg.getActivityId(), msg.getGroupId());
        }
    }
}
